package StudyCode.summary.mycollection;

import java.lang.reflect.Array;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class GetArrayFromObject {

	public static final Class<?>[] ARRAY_PRIMITIVE_TYPES = {int[].class,
			float[].class, double[].class, boolean[].class, byte[].class,
			short[].class, long[].class, char[].class};

	public static Object[] getArray(Object val) {
		Class<?> valKlass = val.getClass();
		Object[] outputArray = null;
		for (Class<?> arrKlass : ARRAY_PRIMITIVE_TYPES) {
			if (valKlass.isAssignableFrom(arrKlass)) {
				int arrlength = Array.getLength(val);
				outputArray = new Object[arrlength];
				for (int i = 0; i < arrlength; ++i) {
					outputArray[i] = Array.get(val, i);
				}
				break;
			}
		}
		if (outputArray == null)
			outputArray = (Object[]) val;
		return outputArray;
	}
}
